package com.scc.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.scc.entity.Questions;
import com.scc.service.IAnswersService;
import com.scc.service.IQuestionsService;

@Controller
public class QuestionsController {
	@Resource
	private IQuestionsService questionsService;
	@Resource
	private IAnswersService answersService;
	@RequestMapping("/")
	public String show(Model model){
		System.out.println("执行了show");
		model.addAttribute("list", questionsService.findAll());
		return "/index";
	}
	
	//restful 占位符{自定义名称}
	@RequestMapping("{page}")
	public String showPage(@PathVariable String page){
		return page;
	}
	
	@RequestMapping("/insert")
	public String insert(Questions ques){
		int index=questionsService.insQuesions(ques);
		if(index>0){
			return "redirect:/";
		}else{
			return "question";
		}
	}
	
	@RequestMapping("showDetails/{id}")
	public String showDetails(@PathVariable long id,Model model){
		model.addAttribute("question", questionsService.selById(id));
		model.addAttribute("answers", answersService.selByQid(id));
		return "show";
	}
}
